<template>
    <el-dialog
    :title="showTitle"
    :visible="showArtDialog"
    destroy-on-close
    width="45%"
    @close="artDialogClose">

    <el-form ref="addArtForm" :rules="addArtFormrules" :model="addArtForm"  label-width="80px" size="small">
        <el-form-item label="文章标题" prop="title">
            <el-input v-model="addArtForm.title" placeholder="请输入文章标签"></el-input>
        </el-form-item>
        <el-form-item label="文章内容" prop="articleBody">
            <quill-editor @blur="$refs.addArtForm.validateField('articleBody')" ref="myQuillEditor" v-model="addArtForm.articleBody" :options="toolOptions" />
        </el-form-item>
        <el-form-item label="视频地址">
            <el-input v-model="addArtForm.videoURL" placeholder="请输入视频地址"></el-input>
        </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
        <el-button @click="artDialogClose">取 消</el-button>
        <el-button type="primary" @click="clickSubmit">确 定</el-button>
    </span>
    </el-dialog>
</template>

<script>
// 富文本样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
// 富文本组件
import { quillEditor } from 'vue-quill-editor'
// ajax
import { add, detail, update } from '@/api/hmmm/articles'

// toolbar工具栏选项配置（默认展示全部）
const toolOptions = {
  modules: {
    toolbar: [
      ['bold', 'italic', 'underline', 'strike'],
      [{ list: 'ordered' }, { list: 'bullet' }],
      ['blockquote', 'code-block'],
      ['image', 'link']
    ]
  }
}
export default {
  components: {
    quillEditor
  },
  name: 'HmmmXxxxInterviewArtdialog',
  props: {
    showArtDialog: {
      type: Boolean,
      required: true
    },
    clickArtInfo: {
      type: Object,
      required: true
    }
  },

  data () {
    return {
      // 表单双向绑定
      addArtForm: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      addArtFormrules: {
        title: [
          { required: true, message: '请输入文章标题！', trigger: 'blur' }
        ],
        articleBody: [
          { required: true, message: '请输入文章内容！', trigger: 'blur' }
        ]
      },
      // 富文本配置项
      toolOptions
    }
  },

  computed: {
    showTitle () {
      return this.addArtForm.id ? '修改文章' : '添加文章'
    }
  },

  methods: {
    // 关闭dailog触发
    artDialogClose () {
      this.$emit('update:showArtDialog', false)
      this.$refs.addArtForm.resetFields()
      this.addArtForm = {
        title: '',
        articleBody: '',
        videoURL: ''
      }
    },
    // 点击确定按钮
    clickSubmit () {
      this.$refs.addArtForm.validate(async valid => {
        if (!valid) return
        if (this.addArtForm.id) {
          await update(this.addArtForm)
          this.$message.success('更新文章内容成功！')
        } else {
          await add(this.addArtForm)
          this.$message.success('添加文章成功！')
        }
        this.$parent.getInterviewList()
        this.artDialogClose()
      })
    },
    // 点击修改触发
    async getArtInfoView () {
      const { data: res } = await detail(this.clickArtInfo)
      this.addArtForm = res
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__footer {
    text-align: right;
}
::v-deep  .ql-container{
    height: 200px;
}
</style>
